package com.jzsk.variable.center.common.utils.prop;


import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Properties文件工具类
 *
 * @author jiangli  2017/9/28
 * @version v1.0
 */
public class PropUtils {

    private static final ConcurrentHashMap<String, Prop> MAP = new ConcurrentHashMap<>();

    private PropUtils() {
    }

    public static Prop use(String fileName) {
        return use(fileName, StandardCharsets.UTF_8.name());

    }

    public static Prop use(String fileName, String encoding) {
        Prop result = MAP.get(fileName);
        if (result == null) {
            result = new Prop(fileName, encoding);
            MAP.put(fileName, result);
        }
        return result;
    }

    public static Prop use(File file) {
        return use(file, StandardCharsets.UTF_8.name());
    }

    public static Prop use(File file, String encoding) {
        Prop result = MAP.get(file.getName());
        if (result == null) {
            result = new Prop(file, encoding);
            MAP.put(file.getName(), result);

        }
        return result;
    }

    public static void remove(String fileName) {
        MAP.remove(fileName);
    }

    public static void clear() {
        MAP.clear();
    }
}
